package com.meixun.xuanke.course.handler;


import com.meixun.xuanke.course.exception.MessageException;
import com.meixun.xuanke.course.util.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public String javaExceptionHandler(Exception ex) {
        //拦截请求异常，返回默认的系统异常失败
        ex.printStackTrace();
        return ResultUtil.getErrorjson("服务器异常，请稍后重试。");
    }

    @ResponseBody
    @ExceptionHandler(value = MessageException.class)
    public String MessageExceptionHandler(MessageException ex) {
        //拦截请求异常，返回自定义异常
        return ResultUtil.getJson(ex.getCode(),"",ex.getMsg());
    }
}
